home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / unzip42.zip / UNZIP.H < prev    next >
C/C++ Source or Header  |  1992-03-19  |  36KB  |  1,033 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   unzip.h
  4.  
  5.   This header file is used by all of the unzip source files.  Its contents
  6.   are divided into seven more-or-less separate sections:  predefined macros,
  7.   OS-dependent includes, (mostly) OS-independent defines, typedefs, function 
  8.   prototypes (or "prototypes," in the case of non-ANSI compilers), macros, 
  9.   and global-variable declarations.
  10.  
  11.   ---------------------------------------------------------------------------*/
  12.  
  13.  
  14.  
  15. /*****************************************/
  16. /*  Predefined, Machine-specific Macros  */
  17. /*****************************************/
  18.  
  19. #if defined(ultrix) || defined(bsd4_2) || defined(sun)
  20. #  if !defined(BSD) && !defined(__SYSTEM_FIVE)
  21. #    define BSD
  22. #  endif /* !BSD && !__SYSTEM_FIVE */
  23. #endif /* ultrix || bsd4_2 || sun */
  24.  
  25. /* [1992.1.21:  ALL machines now "NOTINT16"; NOTINT16 and ULONG_ removed] */
  26. #if 0
  27. #  if defined(mips) || defined(sun) || defined(CRAY)
  28. #    ifndef NOTINT16
  29. #      define NOTINT16
  30. #    endif /* !NOTINT16 */
  31. #  endif /* mips || sun || CRAY */
  32.  
  33. #  if defined(vax) || defined(MSDOS) || defined(__MSDOS__)
  34. #    ifdef NOTINT16
  35. #      undef NOTINT16
  36. #    endif /* NOTINT16 */
  37. #  endif /* vax || MSDOS || __MSDOS__ */
  38. #endif /* 0 */
  39.  
  40. #ifdef CRAY
  41. #  ifdef ZMEM
  42. #    undef ZMEM
  43. #  endif /* ZMEM */
  44. #  ifndef TERMIO
  45. #    define TERMIO
  46. #  endif /* !TERMIO */
  47. #endif /* CRAY */
  48.  
  49. #ifdef CONVEX
  50. #  ifndef TERMIO
  51. #    define TERMIO
  52. #  endif /* !TERMIO */
  53. #endif /* CONVEX */
  54.  
  55. /* the i386 test below is to catch SCO Unix (which has redefinition
  56.  * warnings if param.h is included), but it probably doesn't hurt if
  57.  * other 386 Unixes get nailed, too--are there any 386 BSD systems?
  58.  * param.h is mostly included for "BSD", I think...
  59.  */
  60. #if defined(MINIX) || (defined(i386) && defined(unix))
  61. #  define NO_PARAM_H
  62. #endif /* MINIX || (i386 && unix) */
  63.  
  64. #if defined(__IBMC__) && defined(__32BIT__) && !defined(IBMC32)
  65. #  define IBMC32
  66. #endif /* __IBMC__ && __32BIT__ && !IBMC32 */
  67.  
  68.  
  69.  
  70.  
  71.  
  72. /***************************/
  73. /*  OS-Dependent Includes  */
  74. /***************************/
  75.  
  76. #ifndef MINIX            /* Minix needs it after all the other includes (?) */
  77. #  include <stdio.h>     /* this is your standard header for all C compiles */
  78. #endif
  79. #include <ctype.h>
  80. #include <errno.h>       /* used in mapname() */
  81. #define DECLARE_ERRNO    /* everybody except MSC 6.0 */
  82. #ifdef VMS               /* sigh...you just KNEW someone had to break this.  */
  83. #  include <types.h>     /*  (placed up here instead of in VMS section below */
  84. #  include <stat.h>      /*   because types.h is used in some other headers) */
  85. #else  /* almost everybody */
  86. #  if defined(THINK_C) || defined(MPW) /* for Macs */
  87. #    include <stddef.h>
  88. #  else
  89. #    include <sys/types.h> /* off_t, time_t, dev_t, ... */
  90. #    include <sys/stat.h>  /* Everybody seems to need this. */
  91. #  endif
  92. #endif
  93.  
  94.  
  95. /*---------------------------------------------------------------------------
  96.     Next, a word from our Unix (mostly) sponsors:
  97.   ---------------------------------------------------------------------------*/
  98.  
  99. #ifdef UNIX
  100. #  ifdef AMIGA
  101. #    include <libraries/dos.h>
  102. #  else /* !AMIGA */
  103. #    ifndef NO_PARAM_H
  104. #if 0  /* [GRR: this is an alternate fix for SCO's redefinition bug] */
  105. #      ifdef NGROUPS_MAX
  106. #        undef NGROUPS_MAX     /* SCO bug:  defined again in <param.h> */
  107. #      endif /* NGROUPS_MAX */
  108. #endif /* 0 */
  109. #      include <sys/param.h>   /* conflict with <sys/types.h>, some systems? */
  110. #    endif /* !NO_PARAM_H */
  111. #  endif /* ?AMIGA */
  112.  
  113. #  ifndef BSIZE
  114. #    ifdef MINIX
  115. #      define BSIZE   1024
  116. #    else /* !MINIX */
  117. #      define BSIZE   DEV_BSIZE  /* assume common for all Unix systems */
  118. #    endif /* ?MINIX */
  119. #  endif
  120.  
  121. #  ifndef BSD
  122. #    if !defined(AMIGA) && !defined(MINIX)
  123. #      define NO_MKDIR           /* for mapname() */
  124. #    endif /* !AMIGA && !MINIX */
  125. #    ifndef SKIP_TIME_H          /* temporary fix for VMS gcc and Amdahl cc */
  126. #      include <time.h>          /*  in decryption version */
  127. #    endif /* !SKIP_TIME_H */
  128.      struct tm *gmtime(), *localtime();
  129. #  else   /* BSD */
  130. #    ifndef SKIP_TIME_H          /* temporary fix for Sun 3 cc, etc. */
  131. #      include <sys/time.h>      /*  in decryption version */
  132. #    endif /* !SKIP_TIME_H */
  133. #    include <sys/timeb.h>
  134. #  endif
  135.  
  136. #else   /* !UNIX */
  137. #  define BSIZE   512               /* disk block size */
  138. #endif /* ?UNIX */
  139.  
  140. #if defined(V7) || defined(BSD)
  141. #  define strchr    index
  142. #  define strrchr   rindex
  143. #endif
  144.  
  145. /*---------------------------------------------------------------------------
  146.     And now, our MS-DOS and OS/2 corner:
  147.   ---------------------------------------------------------------------------*/
  148.  
  149. #ifdef __TURBOC__
  150. #  define DOS_OS2             /* Turbo C under DOS, MSC under DOS or OS2    */
  151. #  include <sys/timeb.h>      /* for structure ftime                        */
  152. #  ifndef __BORLANDC__        /* There appears to be a bug (?) in Borland's */
  153. #    include <mem.h>          /*   MEM.H related to __STDC__ and far poin-  */
  154. #  endif                      /*   ters. (dpk)  [mem.h included for memcpy] */
  155. #else                         /* NOT Turbo C...                             */
  156. #  ifdef MSDOS                /*   but still MS-DOS, so we'll assume it's   */
  157. #    ifndef MSC               /*   Microsoft's compiler and fake the ID, if */
  158. #      define MSC             /*   necessary (it is in 5.0; apparently not  */
  159. #    endif                    /*   in 5.1 and 6.0)                          */
  160. #    include <dos.h>          /* _dos_setftime()                            */
  161. #  endif
  162. #  ifdef OS2                  /* stuff for DOS and OS/2 family version */
  163. #    if !defined(IBMC32) && !defined(MSC) && !defined(__BORLANDC__)
  164. #      define MSC             /* assume Microsoft if not ID'd already */
  165. #    endif
  166. #    define INCL_BASE
  167. #    define INCL_NOPM
  168. #    include <os2.h>          /* DosQFileInfo(), DosSetFileInfo()? */
  169. #  endif
  170. #endif
  171.  
  172. #ifdef IBMC32
  173. #  define DOS_OS2
  174. #  define S_IFMT 0xF000
  175. #  define timezone _timezone
  176. #endif
  177.  
  178. #ifdef EMX32
  179. #  define DOS_OS2
  180. #  define __32BIT__
  181. #  define DosGetCtryInfo   DosQueryCtryInfo
  182. #  define DosQFileInfo     DosQueryFileInfo
  183. #  define far
  184. #endif
  185. #ifdef MSC                    /* defined for all versions of MSC now         */
  186. #  define DOS_OS2             /* Turbo C under DOS, MSC under DOS or OS/2    */
  187. #  ifndef __STDC__            /* MSC 5.0 and 5.1 aren't truly ANSI-standard, */
  188. #    define __STDC__ 1        /*   but they understand prototypes...so       */
  189. #  endif                      /*   they're close enough for our purposes     */
  190. #  if defined(_MSC_VER) && (_MSC_VER >= 600)      /* new with 5.1 or 6.0 ... */
  191. #    undef DECLARE_ERRNO      /* errno is now a function in a dynamic link   */
  192. #  endif                      /*   library (or something)--incompatible with */
  193. #endif                        /*   the usual "extern int errno" declaration  */
  194.  
  195. #ifdef DOS_OS2                /* defined for both Turbo C, MSC */
  196. #  include <io.h>             /* lseek(), open(), setftime(), dup(), creat() */
  197. #  ifndef SKIP_TIME_H         /* also included in crypt.c */
  198. #    include <time.h>         /* localtime() */
  199. #  endif /* !SKIP_TIME_H */
  200. #endif
  201.  
  202. /*---------------------------------------------------------------------------
  203.     Followed by some VMS (mostly) stuff:
  204.   ---------------------------------------------------------------------------*/
  205.  
  206. #ifdef VMS
  207. #  ifndef SKIP_TIME_H  /* temporary bugfix for VMS gcc in decryption version */
  208. #    include <time.h>           /* the usual non-BSD time functions */
  209. #  endif /* !SKIP_TIME_H */
  210. #  include <file.h>             /* same things as fcntl.h has */
  211. #  include <rmsdef.h>           /* RMS error codes */
  212. #  include "fatdef.h"           /* RMS-related things used by VMSmunch */
  213. #  include "VMSmunch.h"         /* VMSmunch argument definitions */
  214. #  define UNIX                  /* can share most of same code from now on */
  215. #  define RETURN   return_VMS   /* VMS interprets return codes incorrectly */
  216. #else /* !VMS */
  217. #  define RETURN   return       /* only used in main() */
  218. #  ifdef V7
  219. #    define O_RDONLY  0
  220. #    define O_WRONLY  1
  221. #    define O_RDWR    2
  222. #  else /* !V7 */
  223. #    ifdef MTS
  224. #      include <sys/file.h>     /* MTS uses this instead of fcntl.h */
  225. #      include <timeb.h>
  226. #      ifndef SKIP_TIME_H       /* also included in crypt.c */
  227. #        include <time.h>
  228. #      endif /* !SKIP_TIME_H */
  229. #    else /* !MTS */
  230. #      ifdef COHERENT           /* Coherent 3.10/Mark Williams C */
  231. #        include <sys/fcntl.h>
  232. #        define SHORT_NAMES
  233. #        define tzset  settz
  234. #      else /* !COHERENT */
  235. #        include <fcntl.h>      /* #define O_BINARY 0x8000 (no CR/LF */
  236. #      endif /* ?COHERENT */    /*   translation), as used in open() */
  237. #    endif /* ?MTS */
  238. #  endif /* ?V7 */
  239. #endif /* ?VMS */
  240.  
  241. /*---------------------------------------------------------------------------
  242.     And some Mac stuff for good measure:
  243.   ---------------------------------------------------------------------------*/
  244.  
  245. #ifdef THINK_C
  246. #  define MACOS
  247. #  ifndef __STDC__            /* If Think C hasn't defined __STDC__ ... */
  248. #    define __STDC__ 1        /* make sure it's defined: it needs it */
  249. #  else /* __STDC__ defined */
  250. #    if !__STDC__             /* Sometimes __STDC__ is defined as 0. */
  251. #      undef __STDC__         /* It needs to be 1 or required header */
  252. #      define __STDC__ 1      /* files are not properly included. */
  253. #    endif /* !__STDC__ */
  254. #  endif /* ?defined(__STDC__) */
  255. #  include <unix.h>
  256. #  include "macstat.h"
  257. #endif
  258.  
  259. #ifdef MPW
  260. #  define MACOS
  261. #  include <Files.h>
  262. #  include "macstat.h"
  263. #  define CtoPstr c2pstr
  264. #  define PtoCstr p2cstr
  265. #  ifndef TRUE
  266. #    define TRUE     1
  267. #  endif
  268. #  ifndef FALSE
  269. #    define FALSE    0
  270. #  endif
  271. #  ifdef CR
  272. #    undef  CR
  273. #  endif
  274. #endif
  275.  
  276. /*---------------------------------------------------------------------------
  277.     And finally, some random extra stuff:
  278.   ---------------------------------------------------------------------------*/
  279.  
  280. #if __STDC__
  281. #  include <stdlib.h>      /* standard library prototypes, malloc(), etc. */
  282. #  include <string.h>      /* defines strcpy, strcmp, memcpy, etc. */
  283. #else
  284.    char *malloc();
  285.    char *strchr(), *strrchr();
  286.    long lseek();
  287. #endif
  288.  
  289. #ifdef MINIX
  290. #  include <stdio.h>
  291. #endif
  292.  
  293. #ifdef SHORT_NAMES         /* Mark Williams C, ...? */
  294. #  define extract_or_test_files    xtr_or_tst_files
  295. #  define extract_or_test_member   xtr_or_tst_member
  296. #endif
  297.  
  298.  
  299.  
  300.  
  301.  
  302. /*************/
  303. /*  Defines  */
  304. /*************/
  305.  
  306. #define INBUFSIZ          BUFSIZ   /* same as stdio uses */
  307. #define DIR_BLKSIZ        64       /* number of directory entries per block
  308.                                     *  (should fit in 4096 bytes, usually) */
  309. /*
  310.  * If <limits.h> exists on most systems, should include that, since it may
  311.  * define some or all of the following:  NAME_MAX, PATH_MAX, _POSIX_NAME_MAX,
  312.  * _POSIX_PATH_MAX.
  313.  */
  314. #ifdef DOS_OS2
  315. #  include <limits.h>
  316. #endif /* ?DOS_OS2 */
  317.  
  318. #ifdef _MAX_PATH
  319. #  define FILNAMSIZ       (_MAX_PATH)
  320. #else /* !_MAX_PATH */
  321. #  define FILNAMSIZ       1025
  322. #endif /* ?_MAX_PATH */
  323.  
  324. #ifndef PATH_MAX
  325. #  ifdef MAXPATHLEN                /* defined in <sys/param.h> some systems */
  326. #    define PATH_MAX      MAXPATHLEN
  327. #  else
  328. #    if FILENAME_MAX > 255         /* used like PATH_MAX on some systems */
  329. #      define PATH_MAX    FILENAME_MAX
  330. #    else
  331. #      define PATH_MAX    (FILNAMSIZ - 1)
  332. #    endif
  333. #  endif /* ?MAXPATHLEN */
  334. #endif /* !PATH_MAX */
  335.  
  336. #ifdef ZIPINFO
  337. #  define OUTBUFSIZ       BUFSIZ   /* zipinfo needs less than unzip does    */
  338. #else
  339. #  define OUTBUFSIZ       0x2000   /* unImplode needs power of 2, >= 0x2000 */
  340. #endif
  341.  
  342. #define ZSUFX             ".zip"
  343. #define CENTRAL_HDR_SIG   "\113\001\002"   /* the infamous "PK" signature */
  344. #define LOCAL_HDR_SIG     "\113\003\004"   /*  bytes, sans "P" (so unzip not */
  345. #define END_CENTRAL_SIG   "\113\005\006"   /*  mistaken for zipfile itself) */
  346.  
  347. #define SKIP              0    /* choice of activities for do_string() */
  348. #define DISPLAY           1
  349. #define FILENAME          2
  350. #define EXTRA_FIELD       3
  351.  
  352. #define DOES_NOT_EXIST    -1   /* return values for check_for_newer() */
  353. #define EXISTS_AND_OLDER  0
  354. #define EXISTS_AND_NEWER  1
  355.  
  356. #define DOS_OS2_FAT_      0    /* version_made_by codes (central dir) */
  357. #define AMIGA_            1
  358. #define VMS_              2    /* MAKE SURE THESE ARE NOT DEFINED ON */
  359. #define UNIX_             3    /* THE RESPECTIVE SYSTEMS!!  (like, for */
  360. #define VM_CMS_           4    /* instance, "VMS", or "UNIX":  CFLAGS = */
  361. #define ATARI_            5    /*  -O -DUNIX) */
  362. #define OS2_HPFS_         6
  363. #define MAC_              7
  364. #define Z_SYSTEM_         8
  365. #define CPM_              9
  366. /* #define TOPS20_   10?  (TOPS20_ is to be defined in PKZIP 2.0...)  */
  367. #define NUM_HOSTS         10   /* index of last system + 1 */
  368.  
  369. #define STORED            0    /* compression methods */
  370. #define SHRUNK            1
  371. #define REDUCED1          2
  372. #define REDUCED2          3
  373. #define REDUCED3          4
  374. #define REDUCED4          5
  375. #define IMPLODED          6
  376. #define TOKENIZED         7
  377. #define DEFLATED          8
  378. #define NUM_METHODS       9    /* index of last method + 1 */
  379. /* don't forget to update list_files() appropriately if NUM_METHODS changes */
  380.  
  381. #define DF_MDY            0    /* date format 10/26/91 (USA only) */
  382. #define DF_DMY            1    /* date format 26/10/91 (most of the world) */
  383. #define DF_YMD            2    /* date format 91/10/26 (a few countries) */
  384.  
  385. #define UNZIP_VERSION     20   /* compatible with PKUNZIP 2.0 */
  386. #define VMS_VERSION       42   /* if OS-needed-to-extract is VMS:  can do */
  387.  
  388. /*---------------------------------------------------------------------------
  389.     True sizes of the various headers, as defined by PKWare--so it is not
  390.     likely that these will ever change.  But if they do, make sure both these
  391.     defines AND the typedefs below get updated accordingly.
  392.   ---------------------------------------------------------------------------*/
  393.  
  394. #define LREC_SIZE     26    /* lengths of local file headers, central */
  395. #define CREC_SIZE     42    /*  directory headers, and the end-of-    */
  396. #define ECREC_SIZE    18    /*  central-dir record, respectively      */
  397.  
  398.  
  399. #define MAX_BITS      13                 /* used in unShrink() */
  400. #define HSIZE         (1 << MAX_BITS)    /* size of global work area */
  401.  
  402. #define LF   10   /* '\n' on ASCII machines.  Must be 10 due to EBCDIC */
  403. #define CR   13   /* '\r' on ASCII machines.  Must be 13 due to EBCDIC */
  404.  
  405. #ifdef EBCDIC
  406. #  define ascii_to_native(c)   ebcdic[(c)]
  407. #  define NATIVE    "EBCDIC"
  408. #endif
  409.  
  410. #ifdef AMIGA
  411. #  define FFLUSH    fflush(stderr);
  412. #else /* !AMIGA */
  413. #  if MPW
  414. #    define FFLUSH    putc('\n',stderr);
  415. #  else
  416. #    define FFLUSH
  417. #  endif
  418. #endif /* ?AMIGA */
  419.  
  420. #ifdef CRYPT
  421. #  define OF __
  422. #  define PWLEN        80
  423. #  define DECRYPT(b)   (update_keys(t=((b)&0xff)^decrypt_byte()),t)
  424. #endif /* CRYPT */
  425.  
  426. #ifdef QQ  /* Newtware version */
  427. #  define QCOND    (quietflg < 2)    /* -xq only kills comments */
  428. #  define QCOND2   (!quietflg)       /* for no file comments with -vq[q] */
  429. #else      /* (original) Bill Davidsen version  */
  430. #  define QCOND    (!quietflg)       /* -xq[q] both kill "extracting:" msgs */
  431. #  define QCOND2   (which_hdr)       /* file comments with -v, -vq, -vqq */
  432. #endif
  433.  
  434. #ifndef TRUE
  435. #  define TRUE      1   /* sort of obvious */
  436. #  define FALSE     0
  437. #endif
  438.  
  439. #ifndef SEEK_SET        /* These should all be declared in stdio.h!  But   */
  440. #  define SEEK_SET  0   /*  since they're not (in many cases), do so here. */
  441. #  define SEEK_CUR  1
  442. #  define SEEK_END  2
  443. #endif
  444.  
  445. #ifndef S_IRUSR
  446. #  define S_IRWXU       00700       /* read, write, execute: owner */
  447. #  define S_IRUSR       00400       /* read permission: owner */
  448. #  define S_IWUSR       00200       /* write permission: owner */
  449. #  define S_IXUSR       00100       /* execute permission: owner */
  450. #  define S_IRWXG       00070       /* read, write, execute: group */
  451. #  define S_IRGRP       00040       /* read permission: group */
  452. #  define S_IWGRP       00020       /* write permission: group */
  453. #  define S_IXGRP       00010       /* execute permission: group */
  454. #  define S_IRWXO       00007       /* read, write, execute: other */
  455. #  define S_IROTH       00004       /* read permission: other */
  456. #  define S_IWOTH       00002       /* write permission: other */
  457. #  define S_IXOTH       00001       /* execute permission: other */
  458. #endif /* !S_IRUSR */
  459.  
  460. #ifdef ZIPINFO      /* these are individually checked because SysV doesn't */
  461. #  ifndef S_IFBLK   /*  have some of them, Microsoft C others, etc. */
  462. #    define   S_IFBLK     0060000     /* block special */
  463. #  endif
  464. #  ifndef S_IFIFO  /* in Borland C, not MSC */
  465. #    define   S_IFIFO     0010000     /* fifo */
  466. #  endif
  467. #  ifndef S_IFLNK  /* in BSD, not SysV */
  468. #    define   S_IFLNK     0120000     /* symbolic link */
  469. #  endif
  470. #  ifndef S_IFSOCK  /* in BSD, not SysV */
  471. #    define   S_IFSOCK    0140000     /* socket */
  472. #  endif
  473. #  ifndef S_ISUID
  474. #    define S_ISUID       04000       /* set user id on execution */
  475. #  endif
  476. #  ifndef S_ISGID
  477. #    define S_ISGID       02000       /* set group id on execution */
  478. #  endif
  479. #  ifndef S_ISVTX
  480. #    define S_ISVTX       01000       /* directory permissions control */
  481. #  endif
  482. #  ifndef S_ENFMT
  483. #    define S_ENFMT       S_ISGID     /* record locking enforcement flag */
  484. #  endif
  485. #endif /* ZIPINFO */
  486.  
  487.  
  488.  
  489.  
  490.  
  491. /**************/
  492. /*  Typedefs  */
  493. /**************/
  494.  
  495. #ifndef _BULL_SOURCE                /* Bull has it defined somewhere already */
  496.    typedef unsigned char  byte;     /* code assumes UNSIGNED bytes */
  497. #endif /* !_BULL_SOURCE */
  498.  
  499. typedef char              boolean;
  500. typedef long              longint;
  501. typedef unsigned short    UWORD;
  502. #if !defined(IBMC32) && !defined(EMX32)
  503.    typedef unsigned long  ULONG;
  504. #endif
  505.  
  506. typedef struct min_info {
  507.     unsigned unix_attr;
  508.     unsigned dos_attr;
  509.     int hostnum;
  510.     longint offset;
  511. /*  ULONG crc;  */           /* did need to check decryption (use local now) */
  512.     unsigned encrypted : 1;  /* file encrypted: decrypt before uncompressing */
  513.     unsigned ExtLocHdr : 1;  /* use time instead of CRC for decrypt check */
  514.     unsigned text : 1;       /* file is text or binary */
  515.     unsigned lcflag : 1;     /* convert filename to lowercase */
  516. } min_info;
  517.  
  518. /*---------------------------------------------------------------------------
  519.     Zipfile layout declarations.  If these headers ever change, make sure the
  520.     xxREC_SIZE defines (above) change with them!
  521.   ---------------------------------------------------------------------------*/
  522.  
  523.    typedef byte   local_byte_hdr[ LREC_SIZE ];
  524. #      define L_VERSION_NEEDED_TO_EXTRACT_0     0
  525. #      define L_VERSION_NEEDED_TO_EXTRACT_1     1
  526. #      define L_GENERAL_PURPOSE_BIT_FLAG        2
  527. #      define L_COMPRESSION_METHOD              4
  528. #      define L_LAST_MOD_FILE_TIME              6
  529. #      define L_LAST_MOD_FILE_DATE              8
  530. #      define L_CRC32                           10
  531. #      define L_COMPRESSED_SIZE                 14
  532. #      define L_UNCOMPRESSED_SIZE               18
  533. #      define L_FILENAME_LENGTH                 22
  534. #      define L_EXTRA_FIELD_LENGTH              24
  535.  
  536.    typedef byte   cdir_byte_hdr[ CREC_SIZE ];
  537. #      define C_VERSION_MADE_BY_0               0
  538. #      define C_VERSION_MADE_BY_1               1
  539. #      define C_VERSION_NEEDED_TO_EXTRACT_0     2
  540. #      define C_VERSION_NEEDED_TO_EXTRACT_1     3
  541. #      define C_GENERAL_PURPOSE_BIT_FLAG        4
  542. #      define C_COMPRESSION_METHOD              6
  543. #      define C_LAST_MOD_FILE_TIME              8
  544. #      define C_LAST_MOD_FILE_DATE              10
  545. #      define C_CRC32                           12
  546. #      define C_COMPRESSED_SIZE                 16
  547. #      define C_UNCOMPRESSED_SIZE               20
  548. #      define C_FILENAME_LENGTH                 24
  549. #      define C_EXTRA_FIELD_LENGTH              26
  550. #      define C_FILE_COMMENT_LENGTH             28
  551. #      define C_DISK_NUMBER_START               30
  552. #      define C_INTERNAL_FILE_ATTRIBUTES        32
  553. #      define C_EXTERNAL_FILE_ATTRIBUTES        34
  554. #      define C_RELATIVE_OFFSET_LOCAL_HEADER    38
  555.  
  556.    typedef byte   ec_byte_rec[ ECREC_SIZE+4 ];
  557. /*     define SIGNATURE                         0   space-holder only */
  558. #      define NUMBER_THIS_DISK                  4
  559. #      define NUM_DISK_WITH_START_CENTRAL_DIR   6
  560. #      define NUM_ENTRIES_CENTRL_DIR_THS_DISK   8
  561. #      define TOTAL_ENTRIES_CENTRAL_DIR         10
  562. #      define SIZE_CENTRAL_DIRECTORY            12
  563. #      define OFFSET_START_CENTRAL_DIRECTORY    16
  564. #      define ZIPFILE_COMMENT_LENGTH            20
  565.  
  566.  
  567.    typedef struct local_file_header {                 /* LOCAL */
  568.        byte version_needed_to_extract[2];
  569.        UWORD general_purpose_bit_flag;
  570.        UWORD compression_method;
  571.        UWORD last_mod_file_time;
  572.        UWORD last_mod_file_date;
  573.        ULONG crc32;
  574.        ULONG compressed_size;
  575.        ULONG uncompressed_size;
  576.        UWORD filename_length;
  577.        UWORD extra_field_length;
  578.    } local_file_hdr;
  579.  
  580.    typedef struct central_directory_file_header {     /* CENTRAL */
  581.        byte version_made_by[2];
  582.        byte version_needed_to_extract[2];
  583.        UWORD general_purpose_bit_flag;
  584.        UWORD compression_method;
  585.        UWORD last_mod_file_time;
  586.        UWORD last_mod_file_date;
  587.        ULONG crc32;
  588.        ULONG compressed_size;
  589.        ULONG uncompressed_size;
  590.        UWORD filename_length;
  591.        UWORD extra_field_length;
  592.        UWORD file_comment_length;
  593.        UWORD disk_number_start;
  594.        UWORD internal_file_attributes;
  595.        ULONG external_file_attributes;
  596.        ULONG relative_offset_local_header;
  597.    } cdir_file_hdr;
  598.  
  599.    typedef struct end_central_dir_record {            /* END CENTRAL */
  600.        UWORD number_this_disk;
  601.        UWORD num_disk_with_start_central_dir;
  602.        UWORD num_entries_centrl_dir_ths_disk;
  603.        UWORD total_entries_central_dir;
  604.        ULONG size_central_directory;
  605.        ULONG offset_start_central_directory;
  606.        UWORD zipfile_comment_length;
  607.    } ecdir_rec;
  608.  
  609.  
  610.  
  611.  
  612.  
  613. /*************************/
  614. /*  Function Prototypes  */
  615. /*************************/
  616.  
  617. #ifndef __              /* This is a common macro, but the name "__" was */
  618. #  if __STDC__          /*  swiped from the fine folks at Cray Research, */
  619. #    define __(X)   X   /*  Inc. (thanks, guys!).  Keep interior stuff   */
  620. #  else                 /*  parenthesized...                             */
  621. #    define __(X)   ()
  622. #  endif
  623. #endif
  624.  
  625. /*---------------------------------------------------------------------------
  626.     Functions in unzip.c and/or zipinfo.c:
  627.   ---------------------------------------------------------------------------*/
  628.  
  629. int    usage                     __( (int error) );
  630. int    process_zipfile           __( (void) );
  631. int    find_end_central_dir      __( (void) );
  632. int    process_end_central_dir   __( (void) );
  633. int    list_files                __( (void) );
  634. int    process_cdir_file_hdr     __( (void) );
  635. int    process_local_file_hdr    __( (void) );
  636. int    process_central_dir       __( (void) );
  637. int    long_info                 __( (void) );
  638. int    short_info                __( (void) );
  639. char   *zipinfo_time             __( (UWORD *datez, UWORD *timez) );
  640.  
  641. /*---------------------------------------------------------------------------
  642.     Functions in extract.c:
  643.   ---------------------------------------------------------------------------*/
  644.  
  645. int    extract_or_test_files     __( (void) );
  646. /*
  647.  * static int   store_info               __( (void) );
  648.  * static int   extract_or_test_member   __( (void) );
  649.  */
  650.  
  651. /*---------------------------------------------------------------------------
  652.     Functions in file_io.c and crypt.c:
  653.   ---------------------------------------------------------------------------*/
  654.  
  655. int    open_input_file           __( (void) );
  656. int    readbuf                   __( (char *buf, register unsigned size) );
  657. int    create_output_file        __( (void) );
  658. int    FillBitBuffer             __( (void) );
  659. int    ReadByte                  __( (UWORD *x) );
  660. int    FlushOutput               __( (void) );
  661. /*
  662.  * static int   dos2unix         __( (unsigned char *buf, int len) );
  663.  */
  664. void   set_file_time_and_close   __( (void) );
  665. char   *getp                     __( (char *, char *, int) );
  666.  
  667. int    decrypt_byte              __( (void) );
  668. void   update_keys               __( (int) );
  669. void   init_keys                 __( (char *) );
  670.  
  671. /*---------------------------------------------------------------------------
  672.     Macintosh file_io functions:
  673.   ---------------------------------------------------------------------------*/
  674.  
  675. #ifdef MACOS
  676. /*
  677.  * static int   IsHFSDisk        __( (int wAppVRefNum) );
  678.  */
  679. void   macfstest                 __( (int vrefnum) );
  680. int    mkdir                     __( (char *path) );
  681. #ifndef MCH_MACINTOSH            /* The next prototype upsets Aztec C. */
  682. void   SetMacVol                 __( (char *pch, short wVRefNum) );
  683. #endif
  684. #endif
  685.  
  686. /*---------------------------------------------------------------------------
  687.     Uncompression functions (all internal compression routines, enclosed in
  688.     comments below, are prototyped in their respective files and are invisi-
  689.     ble to external functions):
  690.   ---------------------------------------------------------------------------*/
  691.  
  692. void   inflate                  __( (void) );                  /* inflate.c */
  693. /*
  694.  */
  695.  
  696. void   unImplode                __( (void) );                  /* unimplod.c */
  697. /*
  698.  * static void   ReadLengths     __( (sf_tree *tree) );
  699.  * static void   SortLengths     __( (sf_tree *tree) );
  700.  * static void   GenerateTrees   __( (sf_tree *tree, sf_node *nodes) );
  701.  * static void   LoadTree        __( (sf_tree *tree, int treesize, sf_node *nodes) );
  702.  * static void   LoadTrees       __( (void) );
  703.  * static void   ReadTree        __( (register sf_node *nodes, int *dest) );
  704.  */
  705.  
  706. void   unReduce                 __( (void) );                  /* unreduce.c */
  707. /*
  708.  * static void   LoadFollowers   __( (void) );
  709.  */
  710.  
  711. void   unShrink                 __( (void) );                  /* unshrink.c */
  712. /*
  713.  * static void   partial_clear   __( (void) );
  714.  */
  715.  
  716. /*---------------------------------------------------------------------------
  717.     Functions in match.c, mapname.c, misc.c, etc.:
  718.   ---------------------------------------------------------------------------*/
  719.  
  720. int      match             __( (char *string, char *pattern) );   /* match.c */
  721. /*
  722.  * static BOOLEAN   do_list      __( (register char *string, char *pattern) );
  723.  * static void      list_parse   __( (char **patp, char *lowp, char *highp) );
  724.  * static char      nextch       __( (char **patp) );
  725.  */
  726.  
  727. int      mapname           __( (int create_dirs) );             /* mapname.c */
  728.  
  729. void     UpdateCRC         __( (register unsigned char *s, register int len) );
  730. int      do_string         __( (unsigned int len, int option) );   /* misc.c */
  731. time_t   dos_to_unix_time  __( (unsigned ddate, unsigned dtime) ); /* misc.c */
  732. int      check_for_newer   __( (char *filename) );                 /* misc.c */
  733. int      dateformat        __( (void) );                           /* misc.c */
  734. UWORD    makeword          __( (byte *b) );                        /* misc.c */
  735. ULONG    makelong          __( (byte *sig) );                      /* misc.c */
  736. void     return_VMS        __( (int zip_error) );                  /* misc.c */
  737. #ifdef ZMEM
  738.    char  *memset           __( (register char *buf, register char init, register unsigned int len) );
  739.    char  *memcpy           __( (register char *dst, register char *src, register unsigned int len) );
  740. #endif      /* These guys MUST be ifdef'd because their definition  */
  741.             /*  conflicts with the standard one.  Others (makeword, */
  742.             /*  makelong, return_VMS) don't matter.                 */
  743.  
  744. int      VMSmunch          __( (char *filename, int action, char *extra) );
  745. #ifdef AMIGA
  746.    int   utime             __( (char *file, time_t timep[]) );
  747. #endif
  748.  
  749.  
  750.  
  751.  
  752.  
  753. /************/
  754. /*  Macros  */
  755. /************/
  756.  
  757. #ifndef min    /* MSC defines this in stdlib.h */
  758. #  define min(a,b)   ((a) < (b) ? (a) : (b))
  759. #endif
  760.  
  761.  
  762. #define LSEEK(abs_offset) {longint request=(abs_offset)+extra_bytes,\
  763.    inbuf_offset=request%INBUFSIZ, bufstart=request-inbuf_offset;\
  764.    if(request<0) {fprintf(stderr, SeekMsg, ReportMsg); return(3);}\
  765.    else if(bufstart!=cur_zipfile_bufstart)\
  766.    {cur_zipfile_bufstart=lseek(zipfd,bufstart,SEEK_SET);\
  767.    if((incnt=read(zipfd,(char *)inbuf,INBUFSIZ))<=0) return(51);\
  768.    inptr=inbuf+inbuf_offset; incnt-=inbuf_offset;}\
  769.    else {incnt+=(inptr-inbuf)-inbuf_offset; inptr=inbuf+inbuf_offset;}}
  770.  
  771. /*
  772.  *  Seek to the block boundary of the block which includes abs_offset,
  773.  *  then read block into input buffer and set pointers appropriately.
  774.  *  If block is already in the buffer, just set the pointers.  This macro
  775.  *  is used by process_end_central_dir (unzip.c) and do_string (misc.c).
  776.  *  A slightly modified version is embedded within extract_or_test_files
  777.  *  (unzip.c).  ReadByte and readbuf (file_io.c) are compatible.
  778.  *
  779.  *  macro LSEEK(abs_offset)
  780.  *      longint   abs_offset;
  781.  *  {
  782.  *      longint   request = abs_offset + extra_bytes;
  783.  *      longint   inbuf_offset = request % INBUFSIZ;
  784.  *      longint   bufstart = request - inbuf_offset;
  785.  *
  786.  *      if (request < 0) {
  787.  *          fprintf(stderr, SeekMsg, ReportMsg);
  788.  *          return(3);             /-* 3:  severe error in zipfile *-/
  789.  *      } else if (bufstart != cur_zipfile_bufstart) {
  790.  *          cur_zipfile_bufstart = lseek(zipfd, bufstart, SEEK_SET);
  791.  *          if ((incnt = read(zipfd,inbuf,INBUFSIZ)) <= 0)
  792.  *              return(51);        /-* 51:  unexpected EOF *-/
  793.  *          inptr = inbuf + inbuf_offset;
  794.  *          incnt -= inbuf_offset;
  795.  *      } else {
  796.  *          incnt += (inptr-inbuf) - inbuf_offset;
  797.  *          inptr = inbuf + inbuf_offset;
  798.  *      }
  799.  *  }
  800.  *
  801.  */
  802.  
  803.  
  804. #define SKIP_(length) if(length&&((error=do_string(length,SKIP))!=0))\
  805.   {error_in_archive=error; if(error>1) return error;}
  806.  
  807. /*
  808.  *  Skip a variable-length field, and report any errors.  Used in zipinfo.c
  809.  *  and unzip.c in several functions.
  810.  *
  811.  *  macro SKIP_(length)
  812.  *      UWORD   length;
  813.  *  {
  814.  *      if (length && ((error = do_string(length, SKIP)) != 0)) {
  815.  *          error_in_archive = error;   /-* might be warning *-/
  816.  *          if (error > 1)              /-* fatal *-/
  817.  *              return (error);
  818.  *      }
  819.  *  }
  820.  *
  821.  */
  822.  
  823.  
  824. #define OUTB(intc) {*outptr++=intc; if (++outcnt==OUTBUFSIZ) FlushOutput();}
  825.  
  826. /*
  827.  *  macro OUTB(intc)
  828.  *  {
  829.  *      *outptr++ = intc;
  830.  *      if (++outcnt == OUTBUFSIZ)
  831.  *          FlushOutput();
  832.  *  }
  833.  *
  834.  */
  835.  
  836.  
  837. #define READBIT(nbits,zdest) {if(nbits>bits_left) FillBitBuffer();\
  838.   zdest=(int)(bitbuf&mask_bits[nbits]); bitbuf>>=nbits; bits_left-=nbits;}
  839.  
  840. /*
  841.  * macro READBIT(nbits,zdest)
  842.  *  {
  843.  *      if (nbits > bits_left)
  844.  *          FillBitBuffer();
  845.  *      zdest = (int)(bitbuf & mask_bits[nbits]);
  846.  *      bitbuf >>= nbits;
  847.  *      bits_left -= nbits;
  848.  *  }
  849.  *
  850.  */
  851.  
  852.  
  853. #define PEEKBIT(nbits) ( nbits > bits_left ? (FillBitBuffer(), bitbuf & mask_bits[nbits]) : bitbuf & mask_bits[nbits] )
  854.  
  855.  
  856. #define NUKE_CRs(buf,len) {register int i,j; for (i=j=0; j<len; (buf)[i++]=(buf)[j++]) if ((buf)[j]=='\r') ++j; len=i;}
  857.  
  858. /*
  859.  *  Remove all the ASCII carriage returns from buffer buf (length len),
  860.  *  shortening as necessary (note that len gets modified in the process,
  861.  *  so it CANNOT be an expression).  This macro is intended to be used
  862.  *  BEFORE A_TO_N(); hence the check for CR instead of '\r'.  NOTE:  The
  863.  *  if-test gets performed one time too many, but it doesn't matter.
  864.  *
  865.  *  macro NUKE_CRs( buf, len )
  866.  *    {
  867.  *      register int   i, j;
  868.  *
  869.  *      for ( i = j = 0  ;  j < len  ;  (buf)[i++] = (buf)[j++] )
  870.  *        if ( (buf)[j] == CR )
  871.  *          ++j;
  872.  *      len = i;
  873.  *    }
  874.  *
  875.  */
  876.  
  877.  
  878. #define TOLOWER(str1,str2) {char *ps1,*ps2; ps1=(str1)-1; ps2=(str2); while(*++ps1) *ps2++=(isupper(*ps1))?tolower(*ps1):*ps1; *ps2='\0';}
  879.  
  880. /*
  881.  *  Copy the zero-terminated string in str1 into str2, converting any
  882.  *  uppercase letters to lowercase as we go.  str2 gets zero-terminated
  883.  *  as well, of course.  str1 and str2 may be the same character array.
  884.  *
  885.  *  macro TOLOWER( str1, str2 )
  886.  *    {
  887.  *      register char   *ps1, *ps2;
  888.  *
  889.  *      ps1 = (str1) - 1;
  890.  *      ps2 = (str2);
  891.  *      while ( *++ps1 )
  892.  *        *ps2++ = (isupper(*ps1)) ?  tolower(*ps1)  :  *ps1;
  893.  *      *ps2='\0';
  894.  *    }
  895.  *
  896.  *  NOTES:  This macro makes no assumptions about the characteristics of
  897.  *    the tolower() function or macro (beyond its existence), nor does it
  898.  *    make assumptions about the structure of the character set (i.e., it
  899.  *    should work on EBCDIC machines, too).  The fact that either or both
  900.  *    of isupper() and tolower() may be macros has been taken into account;
  901.  *    watch out for "side effects" (in the C sense) when modifying this
  902.  *    macro.
  903.  */
  904.  
  905.  
  906. #ifndef ascii_to_native
  907.  
  908. #  define ascii_to_native(c)   (c)
  909. #  define A_TO_N(str1)
  910.  
  911. #else
  912.  
  913. #  ifndef NATIVE
  914. #    define NATIVE     "native chars"
  915. #  endif
  916. #  define A_TO_N(str1) { register unsigned char *ps1; for (ps1 = str1; *ps1; ps1++) *ps1 = (ascii_to_native(*ps1)); }
  917.  
  918. /*
  919.  *   Translate the zero-terminated string in str1 from ASCII to the native
  920.  *   character set. The translation is performed in-place and uses the
  921.  *   ascii_to_native macro to translate each character.
  922.  *
  923.  *   macro A_TO_N( str1 )
  924.  *     {
  925.  *       register unsigned char *ps1;
  926.  *
  927.  *       for ( ps1 = str1; *ps1; ps1++ )
  928.  *         *ps1 = ( ascii_to_native( *ps1 ) );
  929.  *     }
  930.  *
  931.  *   NOTE: Using the ascii_to_native macro means that is it the only part of
  932.  *     unzip which knows which translation table (if any) is actually in use
  933.  *     to produce the native character set. This makes adding new character
  934.  *     set translation tables easy insofar as all that is needed is an
  935.  *     appropriate ascii_to_native macro definition and the translation
  936.  *     table itself. Currently, the only non-ASCII native character set
  937.  *     implemented is EBCDIC but this may not always be so.
  938.  */
  939.  
  940. #endif
  941.  
  942.  
  943.  
  944.  
  945.  
  946. /*************/
  947. /*  Globals  */
  948. /*************/
  949.  
  950.    extern int       aflag;
  951. /* extern int       bflag;   reserved */
  952.    extern int       cflag;
  953.    extern int       fflag;
  954.    extern int       jflag;
  955.    extern int       overwrite_none;
  956.    extern int       overwrite_all;
  957.    extern int       force_flag;
  958.    extern int       quietflg;
  959. #ifdef DOS_OS2
  960.    extern int       sflag;
  961. #endif
  962.    extern int       tflag;
  963.    extern int       uflag;
  964.    extern int       V_flag;
  965. #ifdef VMS
  966.    extern int       secinf;
  967. #endif
  968. #ifdef MACOS
  969.    extern int       hfsflag;
  970. #endif
  971.    extern int       process_all_files;
  972.    extern longint   csize;
  973.    extern longint   ucsize;
  974.    extern char      *fnames[];
  975.    extern char      **fnv;
  976.    extern char      sig[];
  977.    extern char      answerbuf[];
  978.    extern min_info  *pInfo;
  979.    extern char      *key;
  980.    extern ULONG     keys[];
  981.  
  982.    extern short     prefix_of[];
  983. #ifdef MACOS
  984.    extern byte      *suffix_of;
  985.    extern byte      *stack;
  986. #else
  987.    extern byte      suffix_of[];
  988.    extern byte      stack[];
  989. #endif
  990.    extern ULONG     crc32val;
  991.    extern ULONG     mask_bits[];
  992.  
  993.    extern byte      *inbuf;
  994.    extern byte      *inptr;
  995.    extern int       incnt;
  996.    extern ULONG     bitbuf;
  997.    extern int       bits_left;
  998.    extern boolean   zipeof;
  999.    extern int       zipfd;
  1000.    extern char      zipfn[];
  1001.    extern longint   extra_bytes;
  1002.    extern longint   cur_zipfile_bufstart;
  1003.    extern byte      *extra_field;
  1004.    extern char      local_hdr_sig[];
  1005.    extern char      central_hdr_sig[];
  1006.    extern char      end_central_sig[];
  1007.    extern local_file_hdr  lrec;
  1008.    extern cdir_file_hdr   crec;
  1009.    extern ecdir_rec       ecrec;
  1010.    extern struct stat     statbuf;
  1011.  
  1012.    extern byte      *outbuf;
  1013.    extern byte      *outptr;
  1014.    extern byte      *outout;
  1015.    extern longint   outpos;
  1016.    extern int       outcnt;
  1017.    extern int       outfd;
  1018.    extern int       disk_full;
  1019.    extern char      filename[];
  1020.  
  1021.    extern char      *EndSigMsg;
  1022.    extern char      *CentSigMsg;
  1023.    extern char      *SeekMsg;
  1024.    extern char      *ReportMsg;
  1025.  
  1026. #ifdef DECLARE_ERRNO
  1027.    extern int       errno;
  1028. #endif
  1029.  
  1030. #ifdef EBCDIC
  1031.    extern byte      ebcdic[];
  1032. #endif
  1033.